package com.example.rvdemo.slide;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.rvdemo.R;

import java.util.List;

public class RVSimpleAdaptar2 extends RecyclerView.Adapter<RVSimpleAdaptar2.InnerHoder> {
    private Context context;
    private List<String> datas;

    public RVSimpleAdaptar2(Context context, List<String> datas) {
        this.context = context;
        this.datas = datas;
    }
    public List<String> getDatas(){
        return datas;
    }

    @NonNull
    @Override
    public InnerHoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.rv_slide_item, parent, false);
        return new InnerHoder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final InnerHoder holder, final int position) {
        holder.textView.setText(datas.get(position));
        if (onDelClickListener != null){
            holder.btn_del.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onDelClickListener.delItem(position);
                }
            });
        }
        Animation animation = AnimationUtils.loadAnimation(holder.itemView.getContext(),R.anim.scale_50_to_100);
        holder.itemView.startAnimation(animation);
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public class InnerHoder extends RecyclerView.ViewHolder {
        TextView textView;
        Button btn_del;
        public InnerHoder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.tv_slide);
            btn_del = itemView.findViewById(R.id.btn_del);
        }
    }
    OnDelClickListener onDelClickListener;

    public void setOnDelClickListener(OnDelClickListener onDelClickListener) {
        this.onDelClickListener = onDelClickListener;
    }

    interface OnDelClickListener{
        void delItem(int pos);
    }
}
